﻿using System.Collections.Generic;
using System.IO;
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.UserModel;

namespace ExcleToWord.Util
{
    public static class WordOutPutUtil
    {
        public static void CreatNewWord(string outputWordPath, Dictionary<string, string> docDic, int docCount, bool userSelectedId )
        {
            XWPFDocument doc = new XWPFDocument();

            string productId = string.Empty;
            foreach (KeyValuePair<string, string> pair in docDic)
            {
                if (pair.Key == "商品id")
                {
                    productId = pair.Value;
                    if (!userSelectedId)  //r如果用户没有勾选ID，则不输出。只是文件命名
                    {
                        continue;
                    }
                   
                }

                XWPFParagraph p1 = doc.CreateParagraph(); //本节提到的所有样式都是基于XWPFRun的
                //p1.IsWordWrapped = true;
                p1.IsWordWrapped = false; //单词是否换行

                AddKeyString(p1, pair.Key);
                AddValueString(p1, pair.Value);
              
            }

            string path = outputWordPath + "\\output";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += string.Format("\\{0}-{1}.doc", docCount, productId);

            FileStream out1 = new FileStream(path, FileMode.Create);
            doc.Write(out1);
            out1.Close();
        }

        private static void AddKeyString(XWPFParagraph p1, string strContent)
        {
            XWPFRun r1 = p1.CreateRun(); //可以把XWPFRun理解成一小段文字的描述对象，
            r1.SetText(strContent + " ： ");
            r1.IsBold = true;
            r1.FontSize = 12;
            // r1.SetFontFamily("宋体", FontCharRange.None); //设置雅黑字体  

            CT_RPr rpr = r1.GetCTR().AddNewRPr();
            CT_Fonts rfonts = rpr.AddNewRFonts();
            rfonts.ascii = "宋体";
            rfonts.eastAsia = "微软雅黑";
            //rpr.AddNewSz().val = (ulong)21;//5号字体
            //rpr.AddNewSzCs().val = (ulong)21;
        }

        private static void AddValueString(XWPFParagraph p1, string strContent)
        {
            XWPFRun r1 = p1.CreateRun(); //可以把XWPFRun理解成一小段文字的描述对象，
            r1.SetText(strContent);
            r1.FontSize = 12;
            // r1.SetFontFamily("宋体", FontCharRange.None); //设置雅黑字体

            CT_RPr rpr = r1.GetCTR().AddNewRPr();
            CT_Fonts rfonts = rpr.AddNewRFonts();
            rfonts.ascii = "宋体";
            rfonts.eastAsia = "微软雅黑";
        }

        private static void Nothing()
        {
            #region 段落格式
            {
                //p1.Alignment = ParagraphAlignment.CENTER;

                //p1.BorderBottom = Borders.Double;
                //p1.BorderTop = Borders.Double;
                //p1.BorderRight = Borders.Double;
                //p1.BorderLeft = Borders.Double;
                //p1.BorderBetween = Borders.Single;

                //p1.VerticalAlignment = TextAlignment.TOP;


                //p2.Alignment = ParagraphAlignment.RIGHT;

                ////BORDERS
                //p2.BorderBottom = Borders.Double;
                //p2.BorderTop = Borders.Double;
                //p2.BorderRight = Borders.Double;
                //p2.BorderLeft = Borders.Double;
                //p2.BorderBetween = Borders.Single;

                //p3.IsWordWrap = true;
                //p3.IsPageBreak = true;
                //p3.Alignment = ParagraphAlignment.BOTH;
                //p3.SpacingLineRule = LineSpacingRule.EXACT;
                //p3.IndentationFirstLine = 600;

                //XWPFRun r4 = p3.CreateRun();
            }

            #endregion

            #region 字体格式

            {
                //r1.IsBold = true;

                //r1.FontFamily = "Courier";
                //r1.SetUnderline(UnderlinePatterns.DotDotDash);
                //r1.SetTextPosition(100);


                //r2.SetText("jumped over the lazy dog");
                //r2.SetStrike(true);
                //r2.FontSize = 20;


                //r3.SetText("and went away");
                //r3.SetStrike(true);
                //r3.FontSize = 20;
                //r3.Subscript = VerticalAlign.SUPERSCRIPT;
                //r3.SetColor("FF0000");

                //r4.SetTextPosition(20);
                //r4.SetText("To be, or not to be: that is the question: "
                //        + "Whether 'tis nobler in the mind to suffer "
                //        + "The slings and arrows of outrageous fortune, "
                //        + "Or to take arms against a sea of troubles, "
                //        + "And by opposing end them? To die: to sleep; ");
                //r4.AddBreak(BreakType.PAGE);
                //r4.SetText("No more; and by a sleep to say we end "
                //        + "The heart-ache and the thousand natural shocks "
                //        + "That flesh is heir to, 'tis a consummation "
                //        + "Devoutly to be wish'd. To die, to sleep; "
                //        + "To sleep: perchance to dream: ay, there's the rub; "
                //        + ".......");
                //r4.IsItalic = true;

                //r5.SetTextPosition(-10);
                //r5.SetText("For in that sleep of death what dreams may come");
                //r5.AddCarriageReturn();
                //r5.SetText("When we have shuffled off this mortal coil,"
                //        + "Must give us pause: there's the respect"
                //        + "That makes calamity of so long life;");
                //r5.AddBreak();
                //r5.SetText("For who would bear the whips and scorns of time,"
                //        + "The oppressor's wrong, the proud man's contumely,");

                //r5.AddBreak(BreakClear.ALL);
                //r5.SetText("The pangs of despised love, the law's delay,"
                //        + "The insolence of office and the spurns" + ".......");
            }

            #endregion
        }
    }
}
